package org.xxpay.pay.channel.juice;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.xxpay.common.http.HttpRequestTemplate;
import org.xxpay.common.util.sign.SignUtils;
import org.xxpay.core.common.constant.PayConstant;
import org.xxpay.core.common.util.DateUtil;
import org.xxpay.core.entity.PayOrder;
import org.xxpay.pay.channel.BasePayment;

import java.util.Map;

/**
 * @Package org.xxpay.pay.channel.juice
 * @Class: JuicepayPaymentService.java
 * @Description:
 * @Author leo
 * @Date 2019/3/29 11:23
 * @Version
 **/
@Component
public class JuicepayPaymentService extends BasePayment {

    @Override
    public String getChannelName() {
        return JuicepayConfig.CHANNEL_NAME;
    }

    @Override
    public JSONObject pay(PayOrder payOrder) {
        String channelId = payOrder.getChannelId();
        JSONObject retObj;
        switch (channelId) {
            case JuicepayConfig.CHANNEL_NAME_JUICE_UNION:
                retObj = doOrderReq(payOrder);
                break;
            default:
                retObj = buildRetObj(PayConstant.RETURN_VALUE_FAIL, "不支持的渠道[channelId="+channelId+"]");
                break;
        }

        return retObj;
    }

    private JSONObject doOrderReq(PayOrder payOrder) {
        JuicepayConfig juicepayConfig = new JuicepayConfig(getPayParam(payOrder));
        // 请求参数
        Map<String, Object> reqPrameters = Maps.newHashMap();
        reqPrameters.put("merCode", juicepayConfig.getMchId());
        reqPrameters.put("merOrderNo", payOrder.getPayOrderId());
        reqPrameters.put("channelCode", juicepayConfig.getPayMode());
        reqPrameters.put("amount", payOrder.getAmount());
        reqPrameters.put("transDate", DateUtil.getCurrentDate());
        reqPrameters.put("buyerName", payOrder.getSubject());
        reqPrameters.put("buyerMobile", payOrder.getMchId());
        reqPrameters.put("bizType", "01");
        reqPrameters.put("goodName", payOrder.getSubject());
        reqPrameters.put("remark", payOrder.getBody());
        reqPrameters.put("notifyURL", payConfig.getNotifyUrl(getChannelName()));
        reqPrameters.put("returnUrl", payOrder.getReturnUrl());
        reqPrameters.put("ip", payOrder.getClientIp());

        String signTxt = SignUtils.parameterText(reqPrameters);
        String sign = SignUtils.MD5.createSign(signTxt, "&key="+juicepayConfig.getPrivateKey(), SignUtils.CHARSET_UTF8);
        reqPrameters.put("sign", sign);
        _log.info("{}请求数据:{}", getChannelName(), JSON.toJSONString(reqPrameters));
        HttpRequestTemplate requestTemplate = new HttpRequestTemplate(null);
        JSONObject origin = requestTemplate.postForObject(juicepayConfig.getRequestUrl(), reqPrameters, JSONObject.class);
        _log.info("{}返回数据:{}", getChannelName(), origin.toJSONString());

        JSONObject retObj = new JSONObject();

        if(StringUtils.equals(origin.getString("code"), JuicepayConfig.RETURN_VALUE_SUCCESS)) {
            // 将订单更改为支付中
            int result = rpcCommonService.rpcPayOrderService.updateStatus4Ing(payOrder.getPayOrderId(), null);
            _log.info("[{}]更新订单状态为支付中:payOrderId={},channelOrderNo={},result={}", getChannelName(), payOrder.getPayOrderId(), "", result);
            // 支付链接地址
            retObj.put("payOrderId", payOrder.getPayOrderId()); // 设置支付订单ID
            JSONObject payInfo = new JSONObject();
            String codeUrl = origin.getString("result");
            payInfo.put("codeUrl", codeUrl); // 二维码支付链接
            payInfo.put("codeImgUrl", payConfig.getPayUrl() + "/qrcode_img_get?url=" + codeUrl + "&widht=200&height=200");
            payInfo.put("payMethod", PayConstant.PAY_METHOD_CODE_IMG);
            retObj.put("payParams", payInfo);
            retObj.put(PayConstant.RETURN_PARAM_RETCODE, PayConstant.RETURN_VALUE_SUCCESS);
            return retObj;
        } else {
            retObj.put("errDes", "下单失败[" + origin.getString("msg") + "]");
            retObj.put(PayConstant.RETURN_PARAM_RETCODE, PayConstant.RETURN_VALUE_FAIL);
            return retObj;
        }
    }


}
